//
//  OwnerViewController.m
//  DaDaBao
//
//  Created by User on 15/8/12.
//  Copyright (c) 2015年 User. All rights reserved.
//
#import "OwnerViewController.h"
#import "AccountManageViewController.h"
#import "GetUserInfoModel.h"
#import "MessageViewController.h"
#import "ChargeViewController.h"
#import "MentionViewController.h"
#import "InviteFriendViewController.h"
#import "TradeDetailViewController.h"
#import "InvestDetailViewController.h"

@interface OwnerViewController ()<UIAlertViewDelegate>
@property (nonatomic,strong)UIScrollView * mainScrollView;//大背景滑动视图
@property (nonatomic,strong)UIView       * moneyView;//资产view
@property (nonatomic,strong)UIView       * middleView;//中间view
@property (nonatomic,strong)UIView       * bottomView;//下半部view
@property (nonatomic,strong)UILabel      * ownMoney;//现有资产
@property (nonatomic,strong)UILabel      * leftMoney;//余额
@property (nonatomic,strong)UILabel      * earnMoney;//赚取的钱
@property (nonatomic,strong)UILabel      * ownMoneyLabel;
@property (nonatomic,strong)UILabel      * leftMoneyLabel;
@property (nonatomic,strong)UILabel      * earnMoneyLabel;
@property (nonatomic,strong)UIView       * lineView;
@property (nonatomic,strong)UIButton     * tipBt;
@property (nonatomic,strong)UIButton     * noDataBt;

@property (nonatomic,strong)GetUserInfoModel *getUserInfoModel;

@end

@implementation OwnerViewController

- (void)getUserInfo
{
    _ownMoney.text = @"0";
    _leftMoney.text = @"0";
    _earnMoney.text = @"0";
    _noDataBt.hidden = TRUE;
    _tipBt.hidden = TRUE;
    [self hideTopView];
    
    [MBProgressHUD showHUDAddedTo:_moneyView animated:TRUE];
    NSDictionary *dict = [[NSDictionary alloc]initWithObjectsAndKeys:[[NSUserDefaults standardUserDefaults]objectForKey:@"token"],@"token", nil];
    [AppGlobalClass PostDataWithNSstring:GET_USER_INFO_URL withDictionary:dict success:^(NSDictionary *dictionary){
        if([[dictionary objectForKey:@"success"] isEqualToNumber:[NSNumber numberWithBool:TRUE]])
        {
            NSArray *arrayList = [[[dictionary objectForKey:@"responseData"]firstObject] objectForKey:@"rows"];
            [_mainScrollView.header endRefreshing];

            if(arrayList.count > 0)
            {
                [self displayTopView];
                _getUserInfoModel = [[GetUserInfoModel alloc]initWithDictionary:[arrayList firstObject]];
                _ownMoney.text = [NSString stringWithFormat:@"%@",_getUserInfoModel.accountBalanceF];
                _leftMoney.text = [NSString stringWithFormat:@"%@",_getUserInfoModel.availableBalanceF];
                _earnMoney.text = [NSString stringWithFormat:@"%@",_getUserInfoModel.assignedIncomeAmount];
            }
            else
            {
                _noDataBt.hidden = FALSE;
            }
        }
        else
        {
            _noDataBt.hidden = FALSE;
        }
        [MBProgressHUD hideAllHUDsForView:_moneyView animated:TRUE];
    } failure:^(NSError *error){
        [_mainScrollView.header endRefreshing];
        _noDataBt.hidden = FALSE;
        
         [MBProgressHUD hideAllHUDsForView:_moneyView animated:TRUE];
} networkerror:^(NSString *networkerror){
    [_mainScrollView.header endRefreshing];
    _tipBt.hidden = FALSE;
     [MBProgressHUD hideAllHUDsForView:_moneyView animated:TRUE];
}];
}

//没网提示
-(UIButton *)tipBt
{
    if (!_tipBt) {
        _tipBt = [[UIButton alloc]initWithFrame:CGRectMake((SCREEN_WIDTH - 116.0f) / 2.0f, (190.0f - 130.0f) / 2.0f, 116.0f, 130.0f)];
        [_tipBt setBackgroundImage:[UIImage imageNamed:@"network_error"] forState:UIControlStateNormal];
        [_tipBt addTarget:self action:@selector(buttonAgainTapped:) forControlEvents:UIControlEventTouchUpInside];
        _tipBt.hidden = TRUE;
    }
    return _tipBt;
}

- (UIButton *)noDataBt
{
    if (!_noDataBt) {
        _noDataBt = [[UIButton alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - 116.0f) / 2.0f, (190.0f - 130.0f) / 2.0f, 116.0f, 130.0f)];
        [_noDataBt addTarget:self action:@selector(buttonAgainTapped:) forControlEvents:UIControlEventTouchUpInside];
        [_noDataBt setImage:[UIImage imageNamed:@"img_nodata"] forState:UIControlStateNormal];
        _noDataBt.hidden = TRUE;
    }
    return _noDataBt;
}

- (void)buttonAgainTapped:(id)sender
{
    [_mainScrollView.header beginRefreshing];
}


#pragma mark alertDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [AppGlobalClass exitLogin];
    
    LoginViewController *loginViewCon = [[LoginViewController alloc]init];
    [self presentViewController:loginViewCon animated:TRUE completion:^{}];
    
    NSArray *listArray = self.navigationController.viewControllers;

    id tabBar = [listArray objectAtIndex:1];
    if([[tabBar class] isSubclassOfClass:[UITabBarController class]])
    {
        UITabBarController *tabBarCon = tabBar;
        tabBarCon.selectedIndex = 0;
    }
    else
    {
        UITabBarController *tabBarCon = [listArray objectAtIndex:2];
        tabBarCon.selectedIndex = 0;
    }
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloginView) name:@"reLogin" object:nil];
    [self getUserInfo];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter]removeObserver:self];
     [MBProgressHUD hideAllHUDsForView:self.view animated:TRUE];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    TopView *topView = [[TopView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, SCREEN_WIDTH, 64.0f)];
    [topView setTitile:@"我的账户"];
    [self.view addSubview:topView];
    
    if(_mainScrollView == nil)
    {
        _mainScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64.0f, SCREEN_WIDTH, SCREEN_HEIGHT-64.0f)];
        _mainScrollView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshTheMainScrollView)];
        _mainScrollView.backgroundColor = [UIColor hexStringToColor:@"#ebebeb"];
        [self.view addSubview:_mainScrollView];
        
        [self createTheMoneyView];//创建资产展示view
        [self createTheMiddleView];//创建中间view
        [self createTheBottomView];//创建下半部view
        
        _mainScrollView.contentSize = CGSizeMake(SCREEN_WIDTH, _moneyView.frame.size.height + _middleView.frame.size.height + _bottomView.frame.size.height + 70);
    }
    
}
-(void)reloginView
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"登录失败，请重新登录" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
    [alert show];
}

//上部控件显示

- (void)displayTopView
{
    _ownMoney.hidden = FALSE;
    _ownMoneyLabel.hidden = FALSE;
    _leftMoney.hidden = FALSE;
    _leftMoneyLabel.hidden = FALSE;
    _earnMoney.hidden = FALSE;
    _earnMoneyLabel.hidden = FALSE;
    _lineView.hidden = FALSE;
}

//隐藏上部控件

- (void)hideTopView
{
    _ownMoney.hidden = TRUE;
    _ownMoneyLabel.hidden = TRUE;
    _leftMoney.hidden = TRUE;
    _leftMoneyLabel.hidden = TRUE;
    _earnMoney.hidden = TRUE;
    _earnMoneyLabel.hidden = TRUE;
    _lineView.hidden = TRUE;
}

#pragma mark--------创建资产展示view
-(void)createTheMoneyView
{
    _moneyView = [[UIView alloc]initWithFrame:CGRectMake(0, 0.0f, SCREEN_WIDTH, 190.0f)];
    _moneyView.backgroundColor = [UIColor whiteColor];
    [_mainScrollView addSubview:_moneyView];
    [_moneyView addSubview:self.tipBt];
    [_moneyView addSubview:self.noDataBt];
//现有资产
    _ownMoney = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 40.0f, 40)];
    _ownMoney.center = CGPointMake(self.view.center.x, 40);
    _ownMoney.textAlignment = NSTextAlignmentCenter;
    _ownMoney.font = [UIFont boldSystemFontOfSize:30];
    _ownMoney.textColor = [UIColor hexStringToColor:RED_COLOR_VALUE];
    [_moneyView addSubview:_ownMoney];
    
    _ownMoneyLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH - 40.0f, 40)];
    _ownMoneyLabel.center = CGPointMake(self.view.center.x , 75);
    _ownMoneyLabel.text = @"现有资产（元）";
    _ownMoneyLabel.font = [UIFont systemFontOfSize:17];
    _ownMoneyLabel.textAlignment = NSTextAlignmentCenter;
    [_moneyView addSubview:_ownMoneyLabel];
//可用余额
    _leftMoney = [[UILabel alloc]initWithFrame:CGRectMake(20.0f, 100.0f, SCREEN_WIDTH / 2.0f - 40.0f, 40)];
    _leftMoney.textAlignment = NSTextAlignmentCenter;
    _leftMoney.font = [UIFont systemFontOfSize:20];
    _leftMoney.textColor = [UIColor hexStringToColor:RED_COLOR_VALUE];
    [_moneyView addSubview:_leftMoney];
    
    _leftMoneyLabel = [[UILabel alloc]initWithFrame:CGRectMake(20.0f, 130.0f, SCREEN_WIDTH / 2.0f - 40.0f, 40)];
    _leftMoneyLabel.text = @"可用余额（元）";
    _leftMoneyLabel.textAlignment = NSTextAlignmentCenter;
    [_moneyView addSubview:_leftMoneyLabel];
    
//净赚取
    _earnMoney = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH / 2.0f + 20.0f, 100.0f, SCREEN_WIDTH / 2.0f - 40.0f, 40)];
    _earnMoney.textAlignment = NSTextAlignmentCenter;
    _earnMoney.font = [UIFont systemFontOfSize:20];
    _earnMoney.textColor = [UIColor hexStringToColor:RED_COLOR_VALUE];
    [_moneyView addSubview:_earnMoney];
    
    _earnMoneyLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH / 2.0f + 20.0f, 130.0f,SCREEN_WIDTH / 2.0f - 40.0f, 40)];
    _earnMoneyLabel.text = @"净赚取（元）";
    _earnMoneyLabel.textAlignment = NSTextAlignmentCenter;
    [_moneyView addSubview:_earnMoneyLabel];
    
//隔线
    _lineView = [[UIView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2.0, 110.0f, 0.5, 50)];
    _lineView.backgroundColor = [UIColor lightGrayColor];
    [_moneyView addSubview:_lineView];
}
#pragma mark--------创建中间view
-(void)createTheMiddleView
{
    _middleView = [[UIView alloc]initWithFrame:CGRectMake(0, 195, SCREEN_WIDTH, 60)];
    [_mainScrollView addSubview:_middleView];
   
    //充值按钮
    UIButton * chargeBtn = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/4-75, 6, 150, 46)];
    chargeBtn.backgroundColor = [UIColor hexStringToColor:RED_COLOR_VALUE];
    chargeBtn.tag = 1001;//用来区分点击的按钮
    chargeBtn.layer.cornerRadius = 5;
    chargeBtn.layer.masksToBounds = TRUE;
    [chargeBtn setTitle:@"充 值" forState:UIControlStateNormal];
    [chargeBtn.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
    [chargeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [chargeBtn addTarget:self action:@selector(buttonGoToViewControllerTapped:) forControlEvents:UIControlEventTouchUpInside];
    [_middleView addSubview:chargeBtn];
    //提现按钮
    UIButton * mentionBtn = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*3/4-75, 6, 150, 46)];
    mentionBtn.tag = 1002;
    mentionBtn.layer.cornerRadius = 5;
    mentionBtn.layer.borderWidth = 1;
    mentionBtn.layer.borderColor = [[UIColor hexStringToColor:RED_COLOR_VALUE] CGColor];
    mentionBtn.backgroundColor = [UIColor hexStringToColor:@"#ebebeb"];
    [mentionBtn setTitle:@"提 现" forState:UIControlStateNormal];
    [mentionBtn.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
    [mentionBtn setTitleColor:[UIColor hexStringToColor:RED_COLOR_VALUE] forState:UIControlStateNormal];
    [mentionBtn addTarget:self action:@selector(buttonGoToViewControllerTapped:) forControlEvents:UIControlEventTouchUpInside];
    [_middleView addSubview:mentionBtn];
}
#pragma mark--------创建下半部view
-(void)createTheBottomView
{
    _bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, 260.0f, SCREEN_WIDTH, 250)];
    _bottomView.backgroundColor = [UIColor whiteColor];
    [_mainScrollView addSubview:_bottomView];
    
    NSArray * titleArray = @[@"投资记录",@"交易明细",@"邀请好友",@"账户设置",@"站内通知"];
    NSArray * imageArray = @[[UIImage imageNamed:@"invest"],[UIImage imageNamed:@"trade"],[UIImage imageNamed:@"friends"],[UIImage imageNamed:@"zhanghu"],[UIImage imageNamed:@"tongzhi"]];
    for (int i = 0 ; i<titleArray.count; i++)
    {
        //背景按钮
        UIButton * backBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, i*50, SCREEN_WIDTH, 50)];
        backBtn.tag = i+1;
        backBtn.backgroundColor = [UIColor clearColor];
        [backBtn addTarget:self action:@selector(buttonGoToViewControllerTapped:) forControlEvents:UIControlEventTouchUpInside];
        [_bottomView addSubview:backBtn];
        //左侧图标
        UIImageView * leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(15, 15.5, 19, 19)];
        leftImageView.image = imageArray[i];
        [backBtn addSubview:leftImageView];
        //标题
        UILabel * titileLabel = [[UILabel alloc]initWithFrame:CGRectMake(50, 0, 100, 50)];
        titileLabel.text = titleArray[i];
        titileLabel.font = [UIFont systemFontOfSize:18];
        [backBtn addSubview:titileLabel];
        if (i < titleArray.count-1)
        {
            //隔线
            UIView * lineView = [[UIView alloc]initWithFrame:CGRectMake(10, (i+1)*50, SCREEN_WIDTH - 20, 0.5)];
            lineView.backgroundColor = [UIColor lightGrayColor];
            [_bottomView addSubview:lineView];
        }
        //右侧箭头图标
        UIImageView * arrowImageView = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 30.0f, 19.5, 6, 11)];
        arrowImageView.image = [UIImage imageNamed:@"arrow"];
        [backBtn addSubview:arrowImageView];
        
    }
}
#pragma mark----------跳转到相应的viewController
-(void)buttonGoToViewControllerTapped:(id)sender
{

    UIButton * button = (UIButton *)sender;
    switch (button.tag) {
        case 1:
        {
            //投资记录界面
            InvestDetailViewController *invest = [[InvestDetailViewController alloc] init];
            [self.navigationController pushViewController:invest animated:TRUE];
        }
            break;
        case 2:
        {
            //交易明细界面
            TradeDetailViewController *teade = [[TradeDetailViewController alloc] init];
            [self.navigationController pushViewController:teade animated:TRUE];
        }
            break;
            
        case 3:
        {
            InviteFriendViewController *inviteVC = [[InviteFriendViewController alloc] init];
            [self.navigationController pushViewController:inviteVC animated:TRUE];
        }
            break;
        case 4:
        {
            AccountManageViewController * vc = [[AccountManageViewController alloc]init];
            [self.navigationController pushViewController:vc animated:TRUE];
        }
            break;
        case 5:
        {
            MessageViewController *messageVC = [[MessageViewController alloc] init];
            [self.navigationController pushViewController:messageVC animated:TRUE];
        }
            break;
        case 1001:
        {
            //充值界面
            ChargeViewController * vc = [[ChargeViewController alloc] init];
            [self.navigationController pushViewController:vc animated:TRUE];
        }
            break;
        case 1002:
        {
            //提现界面
            MentionViewController * vc = [[MentionViewController alloc] initWithAvailableBalance:_getUserInfoModel.availableBalance];
            [self.navigationController pushViewController:vc animated:TRUE];
        }
            break;
        default:
        {
            
        }
            break;
    }
}

#pragma mark-----------刷新方法
-(void)refreshTheMainScrollView
{
    [self getUserInfo];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
